package top.blackr.folivora.common.security.crud.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.blackr.folivora.common.core.R;
import top.blackr.folivora.common.log.annotation.SysLog;
import top.blackr.folivora.common.security.auth.SecUser;
import top.blackr.folivora.common.security.auth.SecurityUtils;
import top.blackr.folivora.common.security.crud.model.vo.SysUserVo;
import top.blackr.folivora.common.security.crud.repository.SysUserRepository;

@Tag(name = "用户")
@RestController
@AllArgsConstructor
@RequestMapping("user")
public class SysUserController {

    private final SysUserRepository userService;

    @GetMapping("pages")
    @SaCheckPermission("user_read")
    @Operation(summary = "用户分页", description = "用户分页")
    public R<?> getPages(Page<SysUserVo> page, SysUserVo params) {
        return R.ok(userService.getPages(page, params));
    }

    @GetMapping("page")
    @SaCheckPermission("user_read")
    @Operation(summary = "分页信息", description = "分页列表")
    public R<Page<SysUserVo>> getUserPage(Page<SysUserVo> page, SysUserVo params) {
        return R.ok(userService.userInfoPage(page, params));
    }

    @PostMapping("resetPwd")
    @SysLog("'用户 ' + #params.username + ' 重置密码'")
    @Operation(summary = "重置密码", description = "重置密码")
    @SaCheckPermission("user_reset_pwd")
    public R<String> resetPwd(@RequestBody SysUserVo params) {
        return R.ok(userService.resetPwd(params));
    }

    @GetMapping("loginInfo")
    @Operation(summary = "登录用户信息", description = "登录用户信息")
    public R<SecUser> loginInfo() {
        SecUser user = SecurityUtils.getUser();
        return R.ok(user);
    }
}
